# 示例：chroma_base.py
# pip install langchain-Chroma
from langchain_chroma import Chroma
from langchain_community.document_loaders import TextLoader
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter

# 加载文档并将其分割成片段
loader = TextLoader(r"D:\uidq0884\Desktop\大模型学习\terms.txt", encoding="UTF-8")
documents = loader.load()
# 将其分割成片段
text_splitter = CharacterTextSplitter(chunk_size=1500, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
# 创建开源嵌入函数
# embedding_function = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
# 将其加载到 Chroma 中
db = Chroma.from_documents(docs, embedding=OpenAIEmbeddings())
# 进行查询
query = "3i是什么?"
docs = db.similarity_search(query)
# 打印结果
print(docs[0].page_content)
